package com.example.origin.finance.utils;

import com.example.origin.finance.enums.TableNameEnum;
import lombok.experimental.UtilityClass;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@UtilityClass
public class TableUtil {
    @SuppressWarnings("unchecked")
    public <T> List<T> find(MongoTemplate mongoTemplate, Query query, TableNameEnum tableNameEnum) {
        Integer count = tableNameEnum.getCount();
        List<String> tableNames = new ArrayList<>();
        if (count == 1) {
            tableNames.add(tableNameEnum.getTable());
        } else {
            for (int i = 0; i < count; i++) {
                tableNames.add(tableNameEnum.getTable() + "_" + i);
            }
        }
        List<T> ts = new ArrayList<>();
        for (String tableName : tableNames) {
            ts.addAll(mongoTemplate.find(query, tableNameEnum.getClazz(), tableName));
        }
        return ts;
    }

}
